package main

import (
	"fmt"
	"gin_easy_starter/configs"
	"gin_easy_starter/internal/container"
	mqConsumer "gin_easy_starter/internal/mq/consumer"
	"gin_easy_starter/internal/router"
	"gin_easy_starter/pkg/shutdown"
)

func main() {

	//accessLogger, err := logger.NewJSONLogger(
	//	logger.WithDisableConsole(),
	//	logger.WithTimeLayout(timeutil.CSTLayout),
	//	logger.WithFileP(configs.ProjectAccessLogFile),
	//)
	//if err != nil {
	//	panic(err)
	//}
	//
	//defer func() {
	//	_ = accessLogger.Sync()
	//}()

	server, _ := router.NewHttpServer()

	// 初始化 DB
	container.InitDB(server)
	//初始化Kafka
	container.InitKafka(server)
	//初始化Redis
	container.InitRedis(server)

	//起一个另外的协程去完成 http服务器的监听
	go func() {
		err := server.Engine.Run(configs.ProjectPort)
		if err != nil {
			fmt.Println("http server startup err")
		}
	}()
	//另外起协程去消费消息
	startKafkaConsumer(server)

	coolShutDown()
}

func startKafkaConsumer(server *router.Server) {
	go mqConsumer.ConsumeMessages(server.KafkaConsumer, "test1", 0, mqConsumer.HandlerRecordOnReceive, mqConsumer.HandlerRecordOnError)
	go mqConsumer.ConsumeMessages(server.KafkaConsumer, "test2", 0, mqConsumer.HandlerDigOnReceive, mqConsumer.HandlerDigOnError)
}

func coolShutDown() {

	shutdown.NewHook().Close(
	//add more need to clear resources
	//关闭 http服务器
	//关闭 mysql
	//关闭 redis
	//关闭 kafka
	)

}
